﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.DBUtility;
using Utils;
using System.Data;
using System.Threading;

public partial class ajax_manage : BasePage
{
    // Methods
    protected override void OnLoad(EventArgs e)
    {
        if (base.GetCookie("admin") == string.Empty)
        {
            base.Response.Write(WebSiteHelper.GetJson("您还没有登录或登录超时，请重新登录！", "alert"));
        }
        else
        {
            string str7;
            string str = Helper.GetString(base.Request["action"], string.Empty);
            string str2 = Helper.GetString(base.Request["id"], string.Empty);
            string str3 = Helper.GetString(base.Request["orderview"], string.Empty);
            string str4 = Helper.GetString(base.Request["rid"], string.Empty);
            string str5 = Helper.GetString(base.Request["nid"], string.Empty);
            string str6 = Helper.GetString(base.Request["orderid"], string.Empty);
            if (str4 != string.Empty)
            {
                str4 = str4.ToLower();
            }
            if ("teamdetail" == str)
            {
                if (!base.CheckUserAuth("team"))
                {
                    base.Response.Write(WebSiteHelper.GetJson("您没有项目管理的操作权限", "alert"));
                    base.Response.End();
                }
                else
                {
                    str7 = WebSiteHelper.LoadPageString("~/ajaxPage/manage_ajax_dialog_teamdetail.aspx?id=" + str2);
                    base.Response.Write(WebSiteHelper.GetJson(str7, "dialog"));
                }
            }
            else if ("userview" == str)
            {
                if (!(base.CheckUserAuth("admin") && base.CheckUserAuth("order")))
                {
                    base.Response.Write(WebSiteHelper.GetJson("您不具备系统管理权限", "alert"));
                }
                else
                {
                    str7 = WebSiteHelper.LoadPageString("~/webpage/ajaxPage/Ajax_user.aspx?Id=" + Helper.GetInt(str2, 0));
                    base.Response.Write(WebSiteHelper.GetJson(str7, "dialog"));
                }
            }
            else if ("orderview" == str)
            {
                if (!base.CheckUserAuth("admin"))
                {
                    base.Response.Write(WebSiteHelper.GetJson("您没有项目管理的操作权限", "alert"));
                    base.Response.End();
                }
                else
                {
                    str7 = WebSiteHelper.LoadPageString(string.Concat(new object[] { "~/webpage/ajaxPage/manage_ajax_dialog_orderview.aspx?orderview=", Helper.GetInt(str3, 0), "&pagenum=", base.Request["pagenum"] }));
                    base.Response.Write(WebSiteHelper.GetJson(str7, "dialog"));
                }
            }
            else
            {
                int @int;
                Pagers pagers;
                DataRow row;
                Maticsoft.Model.Team model;
                Maticsoft.BLL.Coupon coupon;
                int num2;
                int num3;
                Maticsoft.Model.Coupon coupon2;
                List<string> list;
                string str8;
                if ("noticesms" == str)
                {
                    if (!base.CheckUserAuth("team"))
                    {
                        base.Response.Write(WebSiteHelper.GetJson("您没有项目管理权限", "alert"));
                    }
                    else
                    {
                        @int = Helper.GetInt(str5, 0) + 1;
                        pagers = DbHelperSQL.SelectByPager("*", string.Empty, "Create_time asc,couponcreatetime asc", @int, 1, "(select [Order].*,Coupon.Id as couponid,Coupon.Create_time as couponcreatetime,Secret,Expire_time from [Order] left join Coupon on(Coupon.Order_id=[Order].Id) where [Order].Team_id=" + str2 + ") tx1 ", string.Empty, string.Empty);
                        if (@int <= pagers.TotalPage)
                        {
                            Maticsoft.BLL.Team team;
                            row = pagers.MyData.Rows[0];
                            if (row["couponid"].ToString() == string.Empty)
                            {
                                team = new Maticsoft.BLL.Team();
                                model = team.GetModel(Convert.ToInt32(row["Team_id"]));
                                coupon = new Maticsoft.BLL.Coupon();
                                num2 = Convert.ToInt32(row["Quantity"]);
                                num3 = 0;
                                while (num3 < num2)
                                {
                                    coupon2 = new Maticsoft.Model.Coupon();
                                    coupon2.Team_id = Convert.ToInt32(row["Team_id"]);
                                    coupon2.Order_id = Convert.ToInt32(row["id"]);
                                    coupon2.Type = "consume";
                                    coupon2.Credit = model.Credit;
                                    coupon2.Consume = "N";
                                    coupon2.User_id = Convert.ToInt32(row["User_id"]);
                                    coupon2.Create_time = DateTime.Now;
                                    coupon2.Expire_time = model.Expire_time;
                                    coupon2.Partner_id = model.Partner_id;
                                    coupon2.Id = Helper.GetRandomString(12);
                                    coupon2.Secret = Utility.GetCouponpass();
                                    coupon2.Sms = 1;
                                    coupon2.Sms_time = new DateTime?(DateTime.Now);
                                    coupon.Add(coupon2);
                                    list = new List<string>();
                                    list.Add(row["mobile"].ToString());
                                    str8 = "您好，你在" + base.ASSystem.abbreviation + "获得" + model.Title + "的优惠券：" + coupon2.Id + "密码: " + coupon2.Secret + " 有效期至: " + coupon2.Expire_time.ToString("yyyy年MM月dd日") + " 请及时使用";
                                    base.SendSMS(list, str8);
                                    @int++;
                                    num3++;
                                }
                            }
                            else
                            {
                                team = new Maticsoft.BLL.Team();
                                model = team.GetModel(Convert.ToInt32(row["Team_id"]));
                                coupon = new Maticsoft.BLL.Coupon();
                                coupon2 = coupon.GetModel(row["couponid"].ToString());
                                coupon2.Sms++;
                                coupon2.Sms_time = new DateTime?(DateTime.Now);
                                coupon.Update(coupon2);
                                list = new List<string>();
                                list.Add(row["mobile"].ToString());
                                str8 = "您好，你在" + base.ASSystem.abbreviation + "获得" + model.Title + "的优惠券：" + coupon2.Id + "密码: " + coupon2.Secret + " 有效期至: " + coupon2.Expire_time.ToString("yyyy年MM月dd日") + " 请及时使用";
                                base.SendSMS(list, str8);
                                @int++;
                            }
                            base.Response.Write(WebSiteHelper.GetJson(string.Concat(new object[] { "X.misc.noticesms(", str2, ",", @int - 1, ");" }), "eval"));
                        }
                        else
                        {
                            base.Response.Write(WebSiteHelper.GetJson("发送完毕", "alert"));
                        }
                    }
                }
                else
                {
                    Maticsoft.Model.Team team3;
                    int num4;
                    if ("noticesmssubscribe" == str)
                    {
                        if (!base.CheckUserAuth("team"))
                        {
                            base.Response.Write(WebSiteHelper.GetJson("您没有项目管理权限", "alert"));
                        }
                        else
                        {
                            team3 = new Maticsoft.Model.Team();
                            Maticsoft.BLL.Team team4 = new Maticsoft.BLL.Team();
                            team3 = team4.GetModel(Helper.GetInt(str2, 0));
                            @int = Helper.GetInt(str5, 0);
                            if (@int == 0)
                            {
                                @int = 1;
                            }
                            else
                            {
                                if ((@int % 20) > 0)
                                {
                                    @int = (@int / 20) + 1;
                                }
                                else
                                {
                                    @int /= 20;
                                }
                                @int++;
                            }
                            if (@int > 0)
                            {
                                pagers = DbHelperSQL.SelectByPager("*", "City_id=" + team3.City_id + " and Enable='Y'", "id asc", @int, 20, "Smssubscribe", string.Empty, string.Empty);
                                if (@int > pagers.TotalPage)
                                {
                                    base.Response.Write(WebSiteHelper.GetJson("订阅短信发送完毕", "alert"));
                                }
                                else
                                {
                                    DataTable myData = pagers.MyData;
                                    int num9 = ((pagers.CurrentPage - 1) * 20) + myData.Rows.Count;
                                    str5 = num9.ToString();
                                    List<string> mobiles = new List<string>();
                                    for (num4 = 0; num4 < myData.Rows.Count; num4++)
                                    {
                                        mobiles.Add(myData.Rows[num4]["Mobile"].ToString());
                                    }
                                    if (base.SendSMS(mobiles, base.ASSystemArr["sitename"] + "今日团购:" + team3.Title))
                                    {
                                        base.Response.Write(WebSiteHelper.GetJson("X.misc.noticenextsms(" + str2 + "," + str5 + ");", "eval"));
                                    }
                                    else
                                    {
                                        base.Response.Write(WebSiteHelper.GetJson("短信发送失败请检查设置", "alert"));
                                    }
                                }
                            }
                        }
                    }
                    else if ("noticesubscribe" == str)
                    {
                        if (!base.CheckUserAuth("team"))
                        {
                            base.Response.Write(WebSiteHelper.GetJson("您不具备项目管理的权限", "alert"));
                            base.Response.End();
                        }
                        else
                        {
                            if (this.Session["time"] == null)
                            {
                                this.Session.Add("time", DateTime.Now.AddMinutes(-10.0));
                            }
                            DateTime time = (DateTime)this.Session["time"];
                            team3 = new Maticsoft.Model.Team();
                            team3 = new Maticsoft.BLL.Team().GetModel(Helper.GetInt(str2, 0));
                            int num5 = 0;
                            if (base.ASSystem.mailinterval.HasValue)
                            {
                                num5 = base.ASSystem.mailinterval.Value;
                            }
                            @int = Helper.GetInt(str5, 0) + 1;
                            if (@int > 0)
                            {
                                string wherestr = " 1=1";
                                if (team3.City_id != 0)
                                {
                                    wherestr = "City_id=" + team3.City_id;
                                }
                                pagers = DbHelperSQL.SelectByPager("*", wherestr, "id asc", @int, 1, "Mailer", string.Empty, string.Empty);
                                if (@int > pagers.TotalPage)
                                {
                                    base.Response.Write(WebSiteHelper.GetJson("clearTimeout(outtime);alert('订阅邮件发送完毕');", "eval"));
                                }
                                else
                                {
                                    List<string> list3;
                                    string str10;
                                    DataTable table3 = pagers.MyData;
                                    TimeSpan span = (TimeSpan)(DateTime.Now - time);
                                    str5 = (int.Parse(str5) + 1).ToString();
                                    if ((span.Seconds - num5) < 0)
                                    {
                                        Thread.Sleep((int)((num5 - span.Seconds) * 0x3e8));
                                        list3 = new List<string>();
                                        list3.Add(table3.Rows[0]["Email"].ToString());
                                        str10 = string.Empty;
                                        if (base.SendMail(list3, base.ASSystemArr["sitename"] + "今日团购:" + team3.Title, WebSiteHelper.LoadPageString(string.Concat(new object[] { "~/Template/mail_subscribe_team.aspx?mailid=", table3.Rows[0]["id"], "&teamid=", team3.Id })), out str10))
                                        {
                                            base.Response.Write(WebSiteHelper.GetJson(string.Concat(new object[] { "clearTimeout(outtime);X.misc.noticenext(", str2, ",", str5, ");outtime=setTimeout(\"if(window.confirm('超时时间已到，是否继续发送')){X.misc.noticenext(", str2, ",", str5, ")};\",", (num5 + 300) * 0x3e8, ");" }), "eval"));
                                        }
                                        else
                                        {
                                            base.Response.Write(WebSiteHelper.GetJson("clearTimeout(outtime);if(window.confirm('邮件发送失败，是否继续')){X.misc.noticenext(" + str2 + "," + str5 + ")}", "eval"));
                                        }
                                    }
                                    else
                                    {
                                        list3 = new List<string>();
                                        list3.Add(table3.Rows[0]["Email"].ToString());
                                        str10 = string.Empty;
                                        if (base.SendMail(list3, base.ASSystemArr["sitename"] + "今日团购:" + team3.Title, WebSiteHelper.LoadPageString(string.Concat(new object[] { "~/Template/mail_subscribe_team.aspx?mailid=", table3.Rows[0]["id"], "&teamid=", team3.Id })), out str10))
                                        {
                                            base.Response.Write(WebSiteHelper.GetJson(string.Concat(new object[] { "clearTimeout(outtime);X.misc.noticenext(", str2, ",", str5, ");outtime=setTimeout(\"if(window.confirm('超时时间已到，是否继续发送')){X.misc.noticenext(", str2, ",", str5, ")};\",", (num5 + 300) * 0x3e8, ");" }), "eval"));
                                        }
                                        else
                                        {
                                            base.Response.Write(WebSiteHelper.GetJson("clearTimeout(outtime);if(window.confirm('邮件发送失败，是否继续')){X.misc.noticenext(" + str2 + "," + str5 + ")}", "eval"));
                                        }
                                    }
                                    this.Session["time"] = DateTime.Now;
                                }
                            }
                        }
                    }
                    else if (str == "updateorder")
                    {
                        int num7 = Helper.GetInt(base.Request["orderid"], 0);
                        int num8 = 0;
                        if (Helper.GetInt(base.Request["expressid"], 0) == 0)
                        {
                            this.SetError("暂无快递公司");
                        }
                        else
                        {
                            num8 = Helper.GetInt(base.Request["expressid"], 0);
                            if (DbHelperSQL.ExecuteSql(string.Concat(new object[] { "update [order] set Express_id='", num8, "' where id=", num7 })) > 0)
                            {
                                base.Response.Write(WebSiteHelper.GetJson("$('#orderbutton" + str6 + "').val('保存成功')", "eval"));
                            }
                            else
                            {
                                base.Response.Write(WebSiteHelper.GetJson("$('#orderbutton" + str6 + "').val('保存失败')", "eval"));
                            }
                            base.Response.End();
                        }
                    }
                    else if ("categoryedit" == str)
                    {
                        if (!base.CheckUserAuth("admin"))
                        {
                            base.Response.Write(WebSiteHelper.GetJson("您不具备系统管理权限", "alert"));
                        }
                        else if ((base.IsManage && base.CheckUserAuth("admin")) || base.IsSupper)
                        {
                            string str11 = string.Empty;
                            if (base.Request["update"] != null)
                            {
                                str11 = "&update=" + Helper.GetString(base.Request["update"], string.Empty);
                            }
                            str7 = WebSiteHelper.LoadPageString("~/WebPage/ajaxPage/Type.aspx?zone=" + Helper.GetString(base.Request["zone"], string.Empty) + str11);
                            base.Response.Write(WebSiteHelper.GetJson(str7, "dialog"));
                        }
                    }
                    else
                    {
                        switch (str)
                        {
                            case "authorization":
                                if (!base.CheckUserAuth("admin"))
                                {
                                    base.Response.Write(WebSiteHelper.GetJson("您不具备系统管理权限", "alert"));
                                }
                                else
                                {
                                    str7 = WebSiteHelper.LoadPageString("~/WebPage/ajaxPage/Ajax_Manager_Type.aspx?id=" + str2);
                                    base.Response.Write(WebSiteHelper.GetJson(str7, "dialog"));
                                }
                                return;

                            case "teamcoupon":
                                if (!base.CheckUserAuth("team"))
                                {
                                    base.Response.Write(WebSiteHelper.GetJson("您没有项目管理权限", "alert"));
                                }
                                else
                                {
                                    DataTable table = DbHelperSQL.SelectByFilter("*", string.Empty, "id asc", "(select * from (select id,quantity,isnull(num,0) as num,mobile,user_id  from [order] left join (select count(*) as num,order_id from coupon where team_id=" + str2 + " group by order_id)t on([order].id=[t].order_id) where team_id=" + str2 + " and state='pay')t where quantity>num)t");
                                    for (num4 = 0; num4 < table.Rows.Count; num4++)
                                    {
                                        row = table.Rows[num4];
                                        model = new Maticsoft.BLL.Team().GetModel(Convert.ToInt32(row["Team_id"]));
                                        coupon = new Maticsoft.BLL.Coupon();
                                        num2 = Convert.ToInt32(row["Quantity"]);
                                        for (num3 = 0; num3 < num2; num3++)
                                        {
                                            coupon2 = new Maticsoft.Model.Coupon();
                                            coupon2.Team_id = Convert.ToInt32(row["Team_id"]);
                                            coupon2.Order_id = Convert.ToInt32(row["id"]);
                                            coupon2.Type = "consume";
                                            coupon2.Credit = model.Credit;
                                            coupon2.Consume = "N";
                                            coupon2.User_id = Convert.ToInt32(row["User_id"]);
                                            coupon2.Create_time = DateTime.Now;
                                            coupon2.Expire_time = model.Expire_time;
                                            coupon2.Partner_id = model.Partner_id;
                                            coupon2.Id = Helper.GetRandomString(12);
                                            coupon2.Secret = Utility.GetCouponpass();
                                            coupon2.Sms = 1;
                                            coupon2.Sms_time = new DateTime?(DateTime.Now);
                                            coupon.Add(coupon2);
                                            list = new List<string>();
                                            list.Add(row["mobile"].ToString());
                                            str8 = "您好，你在" + base.ASSystem.abbreviation + "获得" + model.Title + "的优惠券：" + coupon2.Id + "密码: " + coupon2.Secret + " 有效期至: " + coupon2.Expire_time.ToString("yyyy年MM月dd日") + " 请及时使用";
                                            base.SendSMS(list, str8);
                                        }
                                    }
                                    base.SetSuccess("自动发券完毕");
                                }
                                return;
                        }
                    }
                }
            }
        }
    }

}